package com.lyp.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.adapters.MyReportDetilsAdapter;
import com.lyp.jsonbean.MyReportBean;
import com.lyp.jsonbean.MyReportDetailsBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//报告详情
public class ReportDetailsActivity extends Activity {

    View v;
    private RecyclerView mRecyclerView;
    private MyReportDetilsAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;
    private List<MyReportDetailsBean.DataBean> mList = new ArrayList<>();
    private ArrayList<MyReportBean.DataBean> bean;
    private ImageView mBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_details);
        initview();
    }

    private void initview(){
        bean = (ArrayList<MyReportBean.DataBean>)getIntent().getExtras().get("databean");
        mRecyclerView = (RecyclerView) findViewById(R.id.report_myrecycleview);
        mBack = (ImageView) findViewById(R.id.iv_baogaoback);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyReportDetilsAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new MyReportDetilsAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Intent intent = new Intent(ReportDetailsActivity.this,ReportDetailsyeActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("databean",mList.get(position));
                LogUtils.i("tag",mList.get(position).getO_id()+bean.get(0).getO_ids()+"fsdfsfsdfsdfd");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        try {
            myreport(bean.get(0).getO_ids());
        } catch (IOException e) {
            e.printStackTrace();
        }

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    //任务列表
    public void myreport(String o_ids) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        YangxixiApi github = retrofit.create(YangxixiApi.class);
        Call<MyReportDetailsBean> call = github.getMyReportDetils(o_ids);
        call.enqueue(new Callback<MyReportDetailsBean>() {
            @Override
            public void onResponse(Call<MyReportDetailsBean> call, final Response<MyReportDetailsBean> response) {
                if (response.body().getResult()== 1 ) {
                    ReportDetailsActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mList.clear();
                            mList.addAll(response.body().getData());
                            mAdapter.notifyDataSetChanged();
                        }
                    });
                }else {
                    ToastUtils.showShort(response.body().getMessage());
                }
            }
            @Override
            public void onFailure(Call<MyReportDetailsBean> call, Throwable t) {
                ToastUtils.showShort(t.getMessage());
            }
        });
    }
}
